<?php
namespace Admin\Model;

use Think\Model;

class UserModel extends Model{
    protected $patchValidate = true;
    protected $_validate = array(
        array('username','require','用户名不能为空！',self::MUST_VALIDATE),
        array('username','checkUserUnique_add','用户名已经被使用！',self::MUST_VALIDATE,'callback','add'),
        array('username','checkUserUnique','用户名已经被使用！',self::MUST_VALIDATE,'callback','edit'),
        array('password','require','密码不能为空！',self::MUST_VALIDATE,'','add'),
        array('password','/^[a-zA-Z0-9_*@#]{6,18}$/','密码必须由6-18位字母、数字或特殊字符( _ , * , # , @ )组成！',self::MUST_VALIDATE,'regex','add'),
        array('repassword','password','确认密码错误！',self::MUST_VALIDATE,'confirm','add'),
        array('sex','require','性别不能为空！',self::MUST_VALIDATE,'','add'),
        array('sex','1,2','性别不合法！',self::MUST_VALIDATE,'in','add'),
        array('id_code','require','身份证号码不能为空！',self::MUST_VALIDATE,'','add'),
        array('id_code','/^[1-9]\d{5}(\d{9}|\d{11}[0-9X])$/','身份证号码不合法！',self::MUST_VALIDATE,'regexp','add'),
        array('tel','require','联系方式不能为空！',self::MUST_VALIDATE),
        array('tel','checkTel','联系方式不合法！',self::MUST_VALIDATE,'callback'),
        array('tel','checkTelUnique_add','联系方式已经被使用！',self::MUST_VALIDATE,'callback','add'),
        array('tel','checkTelUnique','联系方式已经被使用！',self::MUST_VALIDATE,'callback','edit'),
        array('email','require','邮箱不能为空！',self::MUST_VALIDATE),
        array('email','email','邮箱格式不正确！',self::MUST_VALIDATE),
        array('email','checkEmailUnique_add','邮箱已被使用！',self::MUST_VALIDATE,'unique','add'),
        array('email','checkEmailUnique','邮箱已经被使用！',self::MUST_VALIDATE,'callback','edit'),
    );
    protected function checkTel($param){
        if(!preg_match( '/^1[3-9]\d{9}$/', $param ) && !preg_match( '/^0([12]\d{9}|[3-9]\d{9,10})$/', $param )){
            return false;
        }
    }
    protected function checkUserUnique_add($param){
        $where="username='%s'";
        $count=M('User')->where($where,$param)->count();
        if($count){
            return false;
        }
    }
    protected function checkUserUnique($param){
        $where="username='%s' AND id !='%d'";
        $count=M('User')->where($where,$param,$this->id)->count();
        if($count){
            return false;
        }
    }

    protected function checkEmailUnique($param){
        $where="id !='%d' AND email='%s'";
        $count=$this->where($where,$this->id,$param)->count();
        if($count){
            return false;
        }
    }
    protected function checkEmailUnique_add($param){
        $where="email='%s'";
        $count=$this->where($where,$param)->count();
        if($count){
            return false;
        }
    }
    protected function checkTelUnique_add($param){
        $where="tel='%s'";
        $count=M("User")->where($where,$param)->count();
        if($count){
            return false;
        }
    }

    protected function checkTelUnique($param){
        $where="tel='%s' AND id !='%d'";
        $count=M("User")->where($where,$param,$this->id)->count();
        if($count){
            return false;
        }
    }

}

?>